home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / Emacs / spawn.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  7KB  |  226 lines

  1. /* spawn.c */
  2.  
  3. /*
  4.  * The routines in this file are called to create a subjob running a command
  5.  * interpreter. This code is a big fat nothing on CP/M-86. You lose.
  6.  */
  7. #include        <stdio.h>
  8. #include        "ed.h"
  9.  
  10. #if     AMIGA
  11. #define  NEW   1006
  12. #endif
  13.  
  14. #if     VMS
  15. #define EFN     0                               /* Event flag.          */
  16.  
  17. #include        <ssdef.h>                       /* Random headers.      */
  18. #include        <stsdef.h>
  19. #include        <descrip.h>
  20. #include        <iodef.h>
  21.  
  22. extern  int     oldmode[];                      /* In "termio.c"        */
  23. extern  int     newmode[];                      /* In "termio.c"        */
  24. extern  short   iochan;                         /* In "termio.c"        */
  25. #endif
  26.  
  27. #if     MSDOS
  28. #include        <dos.h>
  29. #endif
  30.  
  31. #if     V7
  32. #include        <signal.h>
  33. #endif
  34.  
  35. /*
  36.  * Create a subjob with a copy of the command intrepreter in it. When the
  37.  * command interpreter exits, mark the screen as garbage so that you do a full
  38.  * repaint. Bound to "C-C". The message at the start in VMS puts out a newline.
  39.  * Under some (unknown) condition, you don't get one free when DCL starts up.
  40.  */
  41. spawncli(f, n)
  42. {
  43. #if     AMIGA
  44.         long newcli;
  45.  
  46.         newcli = Open("CON:1/1/639/199/MicroEmacs Subprocess", NEW);
  47.         mlwrite("[Starting new CLI]");
  48.         sgarbf = TRUE;
  49.         Execute("", newcli, 0);
  50.         Close(newcli);
  51.         return(TRUE);
  52. #endif
  53.  
  54. #if     V7
  55.         register char *cp;
  56.         char    *getenv();
  57. #endif
  58. #if     VMS
  59.         movecursor(term.t_nrow, 0);             /* In last line.        */
  60.         mlputs("[Starting DCL]\r\n");
  61.         (*term.t_flush)();                      /* Ignore "ttcol".      */
  62.         sgarbf = TRUE;
  63.         return (sys(NULL));                     /* NULL => DCL.         */
  64. #endif
  65. #if     CPM
  66.         mlwrite("Not in CP/M-86");
  67. #endif
  68. #if     MSDOS
  69.         movecursor(term.t_nrow, 0);             /* Seek to last line.   */
  70.         (*term.t_flush)();
  71.         sys("\\command.com", "");               /* Run CLI.             */
  72.         sgarbf = TRUE;
  73.         return(TRUE);
  74. #endif
  75. #if     V7
  76.         movecursor(term.t_nrow, 0);             /* Seek to last line.   */
  77.         (*term.t_flush)();
  78.         ttclose();                              /* stty to old settings */
  79.         if ((cp = getenv("SHELL")) != NULL && *cp != '\0')
  80.                 system(cp);
  81.         else
  82.                 system("exec /bin/sh");
  83.         sgarbf = TRUE;
  84.         sleep(2);
  85.         ttopen();
  86.         return(TRUE);
  87. #endif
  88. }
  89.  
  90. /*
  91.  * Run a one-liner in a subjob. When the command returns, wait for a single
  92.  * character to be typed, then mark the screen as garbage so a full repaint is
  93.  * done. Bound to "C-X !".
  94.  */
  95. spawn(f, n)
  96. {
  97.         register int    s;
  98.         char            line[NLINE];
  99. #if     AMIGA
  100.         long newcli;
  101.  
  102.         newcli = Open("CON:1/1/639/199/MicroEmacs Subprocess", NEW);
  103.         if ((s=mlreply("CLI command: ", line, NLINE)) != TRUE)
  104.                 return (s);
  105.         Execute(line,0,newcli);
  106.         Close(newcli);
  107.         while ((*term.t_getchar)() != '\r')     /* Pause.               */
  108.                 ;
  109.         sgarbf = TRUE;
  110.         return(TRUE);
  111. #endif
  112. #if     VMS
  113.         if ((s=mlreply("DCL command: ", line, NLINE)) != TRUE)
  114.                 return (s);
  115.         (*term.t_putchar)('\n');                /* Already have '\r'    */
  116.         (*term.t_flush)();
  117.         s = sys(line);                          /* Run the command.     */
  118.         mlputs("\r\n\n[End]");                  /* Pause.               */
  119.         (*term.t_flush)();
  120.         while ((*term.t_getchar)() != '\r')
  121.                 ;
  122.         sgarbf = TRUE;
  123.         return (s);
  124. #endif
  125. #if     CPM
  126.         mlwrite("Not in CP/M-86");
  127.         return (FALSE);
  128. #endif
  129. #if     MSDOS
  130.         if ((s=mlreply("MS-DOS command: ", line, NLINE)) != TRUE)
  131.                 return (s);
  132.         system(line);
  133.         while ((*term.t_getchar)() != '\r')     /* Pause.               */
  134.                 ;
  135.         sgarbf = TRUE;
  136.         return (TRUE);
  137. #endif
  138. #if     V7
  139.         if ((s=mlreply("! ", line, NLINE)) != TRUE)
  140.                 return (s);
  141.         (*term.t_putchar)('\n');                /* Already have '\r'    */
  142.         (*term.t_flush)();
  143.         ttclose();                              /* stty to old modes    */
  144.         system(line);
  145.         sleep(2);
  146.         ttopen();
  147.         mlputs("[End]");                        /* Pause.               */
  148.         (*term.t_flush)();
  149.         while ((s = (*term.t_getchar)()) != '\r' && s != ' ')
  150.                 ;
  151.         sgarbf = TRUE;
  152.         return (TRUE);
  153. #endif
  154. }
  155.  
  156. #if     VMS
  157. /*
  158.  * Run a command. The "cmd" is a pointer to a command string, or NULL if you
  159.  * want to run a copy of DCL in the subjob (this is how the standard routine
  160.  * LIB$SPAWN works. You have to do wierd stuff with the terminal on the way in
  161.  * and the way out, because DCL does not want the channel to be in raw mode.
  162.  */
  163. sys(cmd)
  164. register char   *cmd;
  165. {
  166.         struct  dsc$descriptor  cdsc;
  167.         struct  dsc$descriptor  *cdscp;
  168.         long    status;
  169.         long    substatus;
  170.         long    iosb[2];
  171.  
  172.         status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  173.                           oldmode, sizeof(oldmode), 0, 0, 0, 0);
  174.         if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  175.                 return (FALSE);
  176.         cdscp = NULL;                           /* Assume DCL.          */
  177.         if (cmd != NULL) {                      /* Build descriptor.    */
  178.                 cdsc.dsc$a_pointer = cmd;
  179.                 cdsc.dsc$w_length  = strlen(cmd);
  180.                 cdsc.dsc$b_dtype   = DSC$K_DTYPE_T;
  181.                 cdsc.dsc$b_class   = DSC$K_CLASS_S;
  182.                 cdscp = &cdsc;
  183.         }
  184.         status = LIB$SPAWN(cdscp, 0, 0, 0, 0, 0, &substatus, 0, 0, 0);
  185.         if (status != SS$_NORMAL)
  186.                 substatus = status;
  187.         status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  188.                           newmode, sizeof(newmode), 0, 0, 0, 0);
  189.         if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  190.                 return (FALSE);
  191.         if ((substatus&STS$M_SUCCESS) == 0)     /* Command failed.      */
  192.                 return (FALSE);
  193.         return (TRUE);
  194. }
  195. #endif
  196.  
  197. #if     MSDOS
  198. /*
  199.  * This routine, once again by Bob McNamara, is a C translation of the "system"
  200.  * routine in the MWC-86 run time library. It differs from the "system" routine
  201.  * in that it does not unconditionally append the string ".exe" to the end of
  202.  * the command name. We needed to do this because we want to be able to spawn
  203.  * off "command.com". We really do not understand what it does, but if you don't
  204.  * do it exactly "malloc" starts doing very very strange things.
  205.  */
  206. sys(cmd, tail)
  207. char    *cmd;
  208. char    *tail;
  209. {
  210. #if MWC_86
  211.         register unsigned n;
  212.         extern   char     *__end;
  213.  
  214.         n = __end + 15;
  215.         n >>= 4;
  216.         n = ((n + dsreg() + 16) & 0xFFF0) + 16;
  217.         return(execall(cmd, tail, n));
  218. #endif
  219.  
  220. #if LATTICE
  221.         return forklp(cmd, tail, NULL);
  222. #endif
  223. }
  224. #endif
  225.  
  226.